﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Microsoft.Commerce.Contracts;

namespace MusicWorks.Service.DataContracts
{
   
    public class Basket : BaseCommerceEntity
    {
        public const string ModelNameDefinition = "Basket";

        public Basket()
        {
            this.CommerceEntity = new CommerceEntity(ModelNameDefinition);
        }

        public Basket(CommerceEntity commerceEntity) : base(commerceEntity)
        {
            this.CommerceEntity = commerceEntity;
        }

        public Basket(string name, string userId, int basketType): this()
        {
            this.Name = name;
            this.UserId = userId;
            this.BasketType =basketType;

        }

        public static implicit operator Basket(CommerceEntity commerceEntity)
        {
            return new Basket(commerceEntity);
        }

        public string UserId
        {
            get { return _commerceEntity.GetPropertyValue(PropertyName.UserId) as string; }
            set { _commerceEntity.SetPropertyValue(PropertyName.UserId, value); }
        }

        public int BasketType
        {
            get { return (int) _commerceEntity.GetPropertyValue(PropertyName.BasketType); }
            set { this._commerceEntity.SetPropertyValue(PropertyName.BasketType, value); }
        }

        [DataMember]
        public string Name
        {
            get { return _commerceEntity.GetPropertyValue(PropertyName.Name) as string;}
            set { this._commerceEntity.SetPropertyValue(PropertyName.Name, value); }
        }

        /// <summary>
        /// Gets or sets the line item collection associated with the current basket.
        /// </summary>
        /// <value>The line items.</value>
        [DataMember]
        public CommerceRelationshipList LineItems
        {
            get { return this._commerceEntity.GetPropertyValue(RelationshipName.LineItems) as CommerceRelationshipList; }
            set { this._commerceEntity.SetPropertyValue(RelationshipName.LineItems, value); }
        }

        public decimal TotalPrice //TODO: implement total price getter
        {
            get;
            set;
        }

        [DataMember]
        public decimal TotalQuantityItems
        {
            set {throw new NotImplementedException();}
            get
            {
                if (this.LineItems != null)
                {
                    decimal sum = 0;
                    foreach (CommerceRelationship rel in this.LineItems)
                    {
                        if (rel.Target != null)
                        {
                            decimal quantity = ((decimal?) rel.Target.Properties["Quantity"]) ?? 0;
                            sum += quantity;
                        }
                    }

                    return sum;
                }
                return 0;
            }
        }

        [DataMember]
        public int TotalProductItems
        {
            set { throw new NotImplementedException(); }
            get { return this.LineItems == null ? 0 : this.LineItems.Count(); }
        }

        public CommerceRelationship TargetingContext
        {
            get { return _commerceEntity.GetPropertyValue(RelationshipName.TargetingContext) as CommerceRelationship; }
            set { _commerceEntity.SetPropertyValue(RelationshipName.TargetingContext, value); } 
        }
     
        /// <summary>
        /// Class defining Properties of Items that may be related to a Basket.
        /// </summary>
       public static class RelationshipName
        {

            /// <summary>
            /// The name of the Addresses property for an item related to a Basket.
            /// </summary>
            public const string Addresses = "Addresses";

            /// <summary>
            /// The name of the LineItems property for an item related to a Basket.
            /// </summary>
            public const string LineItems = "LineItems";

            /// <summary>
            /// The name of the Payments property for an item related to a Basket.
            /// </summary>
            public const string Payments = "Payments";

            /// <summary>
            /// The name of the PromoCodes property for an item related to a Basket.
            /// </summary>
            public const string RequestedPromoCodes = "RequestedPromoCodes";

            /// <summary>
            /// The name of the Shipments property for an item related to a Basket.
            /// </summary>
            public const string Shipments = "Shipments";

            /// <summary>
            /// The name of the TargetingContext property for an item related to a Basket.
            /// </summary>
            public const string TargetingContext = "TargetingContext";

        }

        /// <summary>
        /// Names of the properties exposed by this commerce entity.
        /// </summary>
        public class PropertyName : CommerceEntity.PropertyName
        {


            /// <summary>
            /// The name of the BasketLevelDiscountsTotal property for a Basket.
            /// </summary>
            public const string BasketLevelDiscountsTotal = "BasketLevelDiscountsTotal";

            /// <summary>
            /// The name of the BasketType property for a Basket.
            /// </summary>
            public const string BasketType = "BasketType";

            /// <summary>
            /// The name of the BillingCurrency property for a Basket.
            /// </summary>
            public const string BillingCurrency = "BillingCurrency";

            /// <summary>
            /// The name of the DiscountsTotal property for a Basket.
            /// </summary>
            public const string DiscountsTotal = "DiscountsTotal";

            /// <summary>
            /// The name of the HandlingTotal property for a Basket.
            /// </summary>
            public const string HandlingTotal = "HandlingTotal";

            /// <summary>
            /// The name of the LineItemDiscountsTotal property for a Basket.
            /// </summary>
            public const string LineItemDiscountsTotal = "LineItemDiscountsTotal";

            /// <summary>
            /// The name of the Name property for a Basket.
            /// </summary>
            public const string Name = "Name";

            /// <summary>
            /// The name of the OrderNumber property for a Basket.
            /// </summary>
            public const string OrderNumber = "OrderNumber";

            /// <summary>
            /// The name of the ShippingDiscountsTotal property for a Basket.
            /// </summary>
            public const string ShippingDiscountsTotal = "ShippingDiscountsTotal";

            /// <summary>
            /// The name of the ShippingTotal property for a Basket.
            /// </summary>
            public const string ShippingTotal = "ShippingTotal";

            /// <summary>
            /// The name of the Status property for a Basket.
            /// </summary>
            public const string Status = "Status";

            /// <summary>
            /// The name of the SubTotal property for a Basket.
            /// </summary>
            public const string SubTotal = "SubTotal";

            /// <summary>
            /// The name of the TaxTotal property for a Basket.
            /// </summary>
            public const string TaxTotal = "TaxTotal";

            /// <summary>
            /// The name of the Total property for a Basket.
            /// </summary>
            public const string Total = "Total";

            /// <summary>
            /// The name of the UserId property for a Basket.
            /// </summary>
            public const string UserId = "UserId";

        }

    }
}
